/*
 * @Author: wwssaabb
 * @Date: 2021-08-02 16:13:48
 * @LastEditTime: 2021-08-03 16:21:38
 * @FilePath: \react-admin-server\public\test\db.js
 */

const fs = require('fs')
const mongoose=require('mongoose')
const { categories } = require('../../model/categories')

mongoose.connect('mongodb://joker:lx1094577536@8.129.105.10:27017/test',{
  useNewUrlParser:true,
  useUnifiedTopology:true,
  useCreateIndex:true
}).then(()=>console.log('数据库连接成功'))
  .catch(err=>console.log('数据库连接失败',err))

let category_3=mongoose.model('category_3',new mongoose.Schema({
  name:String,
  goods:Array
}))

let category_2=mongoose.model('category_2',new mongoose.Schema({
  name:String,
  children:[{type:mongoose.SchemaTypes.ObjectId,ref:'category_3'}]
}))

let category_1=mongoose.model('category_1',new mongoose.Schema({
  name:String,
  children:[{type:mongoose.SchemaTypes.ObjectId,ref:'category_2'}]
}))


/* category_3.find({}).then(res=>{
  let arr=res
  console.log(res)
  let target_arr=JSON.parse(fs.readFileSync('./target.json','utf-8'))
  let _arr=[]
  target_arr.forEach((i,index)=>{
    _arr.push({name:i.name,children:[]})
    i.children.forEach((_i,_index)=>{
      let id=arr.find(g=>g.name===_i.name)._id
      // delete target_arr[index].children[_index]
      _arr[index].children.push(mongoose.Types.ObjectId(id))
      //target_arr[index].children.push(mongoose.Types.ObjectId(id))
      //target_arr[index].children.splice(_index,1)

    })
  })
  console.log(_arr.length)
  // console.log(_arr[0].children)
  _arr.forEach(i=>{
    category_2.create(i).then(()=>console.log('success'))
  })
}) */

// category_2.find({}).populate('category_3').then(res=>console.log(res))


/* let target_arr=JSON.parse(fs.readFileSync('./target.json'))
category_2.find().then(res=>{
  console.log(res)
  let arr=res
  let _arr=[]
  target_arr.forEach((i,index)=>{
    _arr.push({name:i.name,children:[]})
    arr[index].children.forEach((_i,_index)=>{
      _arr[index].children.push(mongoose.Types.ObjectId(_i._id))
    })
  })
  console.log(_arr)
}) */

/* let categories=mongoose.model('categories',new mongoose.Schema({
  name:String,
  children:Array
})) */

/* let arr=JSON.parse(fs.readFileSync('../category.json','utf-8'))
let _arr=[]
arr.forEach((i,index)=>{
  _arr.push({name:i.name,children:[]})
  i.category.forEach((_i,_index)=>{
    _arr[index].children.push({name:_i.name,children:[]})
    _i.category.forEach((__i,__index)=>{
      _arr[index].children[_index].children.push({name:__i,goods:[],info:{level:3,parent:{name:_i.name,index:_index},grandpa:{name:i.name,index}}})
    })
  })
}) */
// fs.writeFileSync('./category0.json',JSON.stringify(_arr))
// categories.
/* _arr.forEach(i=>{

}) */

/* categories.find().then(res=>{
  let arr=res
  arr.forEach((i,index)=>{
    i.children.forEach((_i,_index)=>{
      let attr_id='children.'+_index+'.id'
      categories.updateOne({_id:i._id},{$set:{[attr_id]:i._id+'_c2_'+_index}}).then(res=>console.log(res,attr_id,i._id)).catch(err=>console.log(err))
      _i.children.forEach((__i,__index)=>{
        let attr_id='children.'+_index+'.children.'+__index+'.id'
        console.log(attr_id)
        categories.updateOne({_id:i._id},{$set:{[attr_id]:i._id+'_c3_'+__index}}).then(res=>console.log(res,attr_id,i._id)).catch(err=>console.log(err))
      })
    })
  })
})
 */
// categories.updateOne({_id:'610899a761dbd8fd6f1153bb'},{$set:{'children.0.children.0.info.grandpa.id':'610899a761dbd8fd6f1153bb'}}).then(res=>console.log(res))

//categories.find({_id:'610899a761dbd8fd6f1153cb'}).then(res=>console.log(res[0].children[0].children))

/* let index_0=JSON.parse(fs.readFileSync('../category.json','utf-8'))

let newIndex_0={name:index_0[0].name,children:[]}
index_0[0].category.forEach((i,index)=>{
  newIndex_0.children.push({name:i.name,children:[],id:'610899a761dbd8fd6f1153bb_c2_'+index})
  i.category.forEach((_i,_index)=>{
    newIndex_0.children[index].children.push({
      name:_i,
      id:'610899a761dbd8fd6f1153bb_c3_'+_index,
      goods:[],
      info:{
        level:3,
        parent:{name:i.name,index:index},
        grandpa:{name:index_0[0].name,index:0,id:'610899a761dbd8fd6f1153bb'}
      }
    })
  })
})
console.log(newIndex_0)
categories.replaceOne({_id:'610899a761dbd8fd6f1153bb'},newIndex_0).then(res=>console.log(res))
  .catch(err=>console.log(err)) */

/* categories.findOne({_id:'610899a761dbd8fd6f1153bb'}).then(res=>{
  console.log(res)
  let _parent=res.children.map((i,index)=>{
    return i.children.length-1
  })
  console.log(_parent)
})
 */
